package model;

import java.util.ArrayList;
import java.util.Collections;


public class Dealer {
	
	private Deck deck;
	
	public Dealer(){
	}
	
	public void dealCards(ArrayList<Player> playingList){
		for(Player player : playingList) {
			for(int i=0; i<2; i++) {
				player.getHand().getCardList().add(this.draw());
			}
		}
	}
	
	public ArrayList<Card> dealFlop(){
		ArrayList<Card> cardList = new ArrayList<Card>();
		for(int i=0; i<3; i++)
			cardList.add(draw());
		return cardList;
	}
	
	public Card draw(){
		Card card;
		card = getDeck().getCardList().get(0);
		getDeck().getCardList().remove(getDeck().getCardList().get(0));
		return card;
	}
	
	public void assignBlinds(ArrayList<Player> playingList){
		int prevDealerButtonCount = getPreviousDealerButton(playingList);
		for(int i=0; i<playingList.size(); i++){	//disable all previous blinds.
			playingList.get(i).setDealerButton(false);
			playingList.get(i).setSmallBlind(false);
			playingList.get(i).setBigBlind(false);
		}
		
		
		int newDealerButtonCount = assignDealerButton(playingList, prevDealerButtonCount);
		assignSmallBlind(newDealerButtonCount, playingList);
		assignBigBlind(newDealerButtonCount, playingList);
	}
	
	
	public int assignDealerButton(ArrayList<Player> playingList, int dealerButton){
		int newDealerButtonCount = dealerButton + 1;
		
		if(newDealerButtonCount>playingList.size())
			newDealerButtonCount = 0;
		playingList.get(newDealerButtonCount).setDealerButton(true);
		return newDealerButtonCount;
	}
	
	public void assignSmallBlind(int dealerButtonCount, ArrayList<Player> playingList){
		int smallBlind = dealerButtonCount+1;
		if(smallBlind==playingList.size())
			smallBlind = 0;
		
		playingList.get(smallBlind).setSmallBlind(true);
	}
	
	public void assignBigBlind(int dealerButtonCount, ArrayList<Player> playingList){
		int smallBlind = dealerButtonCount+1;
		if(smallBlind==playingList.size())
			smallBlind = 0;
		
		int bigBlind = smallBlind+1;
		if(bigBlind==playingList.size())
			bigBlind = 0;
		
		playingList.get(bigBlind).setBigBlind(true);
	}
	
	
	public int getPreviousDealerButton(ArrayList<Player> playingList){
		int dealerButtonCount = -1;
		for(int i=0; i<playingList.size(); i++){
			if(playingList.get(i).isDealerButton()){
				dealerButtonCount = i;
				i = playingList.size()+1; //break the loop
			}	
		}
		return dealerButtonCount;
	}
	
	public void shuffle(){
		Collections.shuffle(deck.getCardList());
	}
	
	public void score(Hand hand){
		//function that will be used for each player joining the showdown. it adds score to the player's hand
		checkForStraightFlush(hand);
	}
	
	public void checkForStraightFlush(Hand hand){

	}
	
	public void checkForQuadro(Hand hand){
		
	}
	
	public void checkForFullHouse(Hand hand){
		
	}
	
	public void checkForFlush(Hand hand){
		int suiteCounter = 0;
		String suit = null;
		//dunno if a boolean should still be here
		for(int x=0; x<7 ; x++) {
			for(int y=0; y<7; y++){
				if(hand.getCardList().get(x).getSuit().equals(hand.getCardList().get(y).getSuit())){
					suiteCounter++;
					if(suiteCounter>=5){
						suit = hand.getCardList().get(x).getSuit();	//saving the suite that completed 5 pairs
						y = 7; x = 7;	//breaking the loop
					}
				}
			}	
		}
	}
	
	public void checkForStraight(Hand hand){
		
	}
	
	public void checkForTwopair(Hand hand){
		
	}
	
	public void checkForTrio(Hand hand){
		
	}
	
	public void checkForPair(Hand hand){
		for(int x = 0 ; x<6 ; x++){
			for(int y = x+1 ; y<7 ; y++){
				if(hand.getCardList().get(x).getFace().equals(hand.getCardList().get(y).getFace())){
					//some code here for saying it found a pair
				}
			}
		}
	}
	
	public void setDeck(Deck deck) {
		this.deck = deck;
	}
	public Deck getDeck() {
		return deck;
	}
	
}
